﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace DarwinIsDead.Traits
{
    public class Shield: Trait
    {
        private float rechargeTime;
        private int damageCap;
        private float counter;
        private SpriteFont font;
        private SoundEffect hitSound;

        public Shield(Zombie owner, World world) 
            : base(owner, world){

            rechargeTime = 5f; // Shield takes 5 seconds to recharge
            damageCap = 10; // Damage capacity of the shield is 10 HP
            counter = 0f;

            font = World.Game.Content.Load<SpriteFont>("Fonts\\Courier New");
            hitSound = World.Game.Content.Load<SoundEffect>("Sounds\\sndMegaHit");
        }

        public override Object Clone() {
            Shield ret = new Shield(Owner, World);
            ret.Level = Level;
            return ret;
        }

        protected override void AdjustLevel(int level)
        {
            if (rechargeTime > 0)
            {
                rechargeTime -= 1f;
            }

            damageCap *= level;
        }

        public override void Collision(Entity other, Rectangle overlap) {
            if (other is Bullet) {
                Bullet bullet = (Bullet)other;
                if (bullet.Friendly) {
                    hitSound.Play();
                }
            }
        }

        protected override void InternalUpdate(GameTime gameTime)
        {
            float dt = gameTime.ElapsedGameTime.Milliseconds / 1000f;

            counter += dt;
            if (counter > rechargeTime)
            {
                if (Owner.Health < (Owner.MaxHealth - damageCap))
                {
                    Owner.Health += damageCap;
                }
                counter = 0f;
            }

        }

        protected override void InternalDraw(SpriteBatch spriteBatch, Color color)
        {
            Vector2 drawPosition = new Vector2(Owner.Position.X, Owner.Position.Y);
            drawPosition.Y -= 16f;  // Draw the legs a little lower on the body

            spriteBatch.DrawString(font, "Shield " + Owner.Health, drawPosition, color);
        }
    }
}
